<?php
/**
 * 巡检功能模型
 */
class inspectionModel extends RelationModel {
	
    //发起关联，给后台使用，前台API 不需要使用这个
    protected $_link = array(
        'user' => array(
            'mapping_type' => BELONGS_TO,
            'class_name' => 'admin',
            'foreign_key' => 'userId',
            'as_fields'=>'userName,realName',
        ),
        'store' => array(
            'mapping_type' => BELONGS_TO,
            'class_name' => 'store',
            'foreign_key' => 'storeId',
            'as_fields'=>'name,ctms,channel,regionId',
        ),  
    );  
        
    public function setStoreLinkCondition($str){
        $this->_link['store']['condition'] = $str;
    }    
        
	/**
	 * 获得巡检表表单字段
	 */
	public function getFormField($modular = 'inspection', $inspectionId = 0) {
		$res = array();
		$attribute = D('attribute')->getAttribute($modular);
		foreach($attribute as $row){
			if($inspectionId && $modular == 'inspection'){
				$val = D('attributevar')->where(array('pid'=>$inspectionId ,'optionid'=>$row['id']))->find();
			}
			$row['value'] = isset($val['value']) ? $val['value'] : '';
			$res[] = $row;
		}
		return $res;
	}
	
	/**
	 * 获得指定巡检表详情
	 * 
	 * @param int $id 巡检记录id
	 */
	public function getFormDetail($id){
		$data = array();
		$res = D('attributevar')->field('var.*, attr.name, attr.description,attr.type')->alias('var')
						->join(C('DB_PREFIX')."attribute AS attr ON attr.id = var.optionid")
						->where("var.modular='inspection' AND var.pid={$id}")->select();
		if($res){
			$data['data'] = $res;
			foreach ($res as $row){
				$data['totalScore'] += $row['value'];
			}
		}
		return $data;
	}
	
	/**
	 * 获得巡检记录图片
	 * @param int $id 巡检记录id
	 * @return array
	 */
	public function getInspectionImages($id){
		$data = array();
		$res = D('inspection')->where(array('id'=>$id))->find();
		if($res && $res['images']){
			$images = unserialize($res['images']);
			foreach($images as $key=>$val){
				foreach($val as $row){
					$data[] = ltrim($row, '/');
				}
			}
		}
		return $data;
	}
	
}

/* End of file inspectionModel.class.php */